added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSCpuUsage / CpuUsageMonitorBase.cs
blob18d9fa3265d4766917f4b606a953d47e6f39f99d
1 /****************************** Module Header ******************************\
2 * Module Name: CpuUsageMonitorBase.cs
3 * Project: CSCpuUsage
4 * Copyright (c) Microsoft Corporation.
5 *
6 * This is the base class of ProcessCpuUsageMonitor and TotalCpuUsageMonitor. It
7 * supplies basic fields/events/functions/interfaces of the monitors, such as Timer,
8 * PerformanceCounter and IDisposable interface.
9 *
11 * This source is subject to the Microsoft Public License.
12 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 * All other rights reserved.
15 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
16 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
18 \***************************************************************************/
20 using System;
21 using System.Collections.Generic;
22 using System.Diagnostics;
23 using System.Threading;
25 namespace CSCpuUsage
27 public abstract class CpuUsageMonitorBase : IDisposable
29 object locker = new object();
31 // Specify whether this instance is disposed.
32 bool disposed = false;
34 // The timer is used to get the performance counter value every few seconds.
35 Timer timer;
37 // The CPU usage performance counter will be initialized in ProcessCpuUsageMonitor
38 // and TotalCpuUsageMonitor.
39 protected PerformanceCounter cpuPerformanceCounter = null;
41 // The max length of the CpuUsageValueArray.
42 int valueArrayCapacity;
44 // The list is used to store the values.
45 List<double> cpuUsageValueArray;
47 /// <summary>
48 /// Occurred if there is a new added value.
49 /// </summary>
50 public event EventHandler<CpuUsageValueArrayChangedEventArg> CpuUsageValueArrayChanged;
53 public event EventHandler<ErrorEventArgs> ErrorOccurred;
55 /// <summary>
56 /// Initialize the timer and performance counter.
57 /// </summary>
58 /// <param name="timerPeriod">
59 /// If this value is no more than 0, then the timer will not be enabled.
60 /// </param>
61 /// <param name="valueArrayCapacity">
62 /// The max length of the CpuUsageValueArray.
63 /// </param>
64 /// <param name="categoryName">
65 /// The name of the performance counter category (performance object) with which
66 /// this performance counter is associated.
67 /// </param>
68 /// <param name="counterName">
69 /// The name of the performance counter.
70 /// </param>
71 /// <param name="instanceName">
72 /// The name of the performance counter category instance, or an empty string (""),
73 /// if the category contains a single instance.
74 /// </param>
75 public CpuUsageMonitorBase(int timerPeriod, int valueArrayCapacity,
76 string categoryName, string counterName, string instanceName)
79 // Initialize the PerformanceCounter.
80 this.cpuPerformanceCounter =
81 new PerformanceCounter(categoryName, counterName, instanceName);
83 this.valueArrayCapacity = valueArrayCapacity;
84 cpuUsageValueArray = new List<double>();
86 if (timerPeriod > 0)
89 // Delay the timer to invoke callback.
90 this.timer = new Timer(new TimerCallback(Timer_Callback),
91 null, timerPeriod, timerPeriod);
95 /// <summary>
96 /// The method to be executed in the timer callback.
97 /// </summary>
98 void Timer_Callback(object obj)
100 lock (locker)
103 // Clear the list.
104 if (this.cpuUsageValueArray.Count > this.valueArrayCapacity)
106 this.cpuUsageValueArray.Clear();
111 double value = GetCpuUsage();
113 if (value < 0)
115 value = 0;
117 if (value > 100)
119 value = 100;
122 this.cpuUsageValueArray.Add(value);
124 double[] values = new double[cpuUsageValueArray.Count];
125 cpuUsageValueArray.CopyTo(values, 0);
127 // Raise the event.
128 this.OnCpuUsageValueArrayChanged(
129 new CpuUsageValueArrayChangedEventArg() { Values = values });
131 catch (Exception ex)
133 this.OnErrorOccurred(new ErrorEventArgs { Error = ex });
138 /// <summary>
139 /// Get current CPU usage.
140 /// </summary>
141 protected virtual double GetCpuUsage()
143 if (!IsInstanceExist())
145 throw new ApplicationException(
146 string.Format("The instance {0} is not available. ",
147 this.cpuPerformanceCounter.InstanceName));
151 double value = cpuPerformanceCounter.NextValue();
152 return value;
155 /// <summary>
156 /// Child class may override this method to determine whether the instance exists.
157 /// </summary>
158 /// <returns></returns>
159 protected virtual bool IsInstanceExist()
161 return true;
164 /// <summary>
165 /// Raise the CpuUsageValueArrayChanged event.
166 /// </summary>
167 protected virtual void OnCpuUsageValueArrayChanged(CpuUsageValueArrayChangedEventArg e)
169 if (this.CpuUsageValueArrayChanged != null)
171 this.CpuUsageValueArrayChanged(this, e);
175 /// <summary>
176 /// Raise the ErrorOccurred event.
177 /// </summary>
178 protected virtual void OnErrorOccurred(ErrorEventArgs e)
180 if (this.ErrorOccurred != null)
182 this.ErrorOccurred(this, e);
186 // Release the resources.
187 public void Dispose()
189 Dispose(true);
190 GC.SuppressFinalize(this);
193 protected virtual void Dispose(bool disposing)
195 // Protect from being called multiple times.
196 if (disposed)
198 return;
201 if (disposing)
203 if (timer != null)
205 timer.Dispose();
208 if (cpuPerformanceCounter != null)
210 cpuPerformanceCounter.Dispose();
212 disposed = true;